package com.yyk.yykywd.mvvm.model

import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.MeetingBean
import com.yyk.yykywd.mvvm.entity.MeetingDetailBean
import com.yyk.yykywd.network.BusinessApi
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 文件描述：
 * Created by guo on 2019-12-03.
 */
class MeetingModel {
    suspend fun getMeetingList(type: String, page: String, num: String, getMeetings: GetMeetings) {
        val newMap = getNewMap()
        putPage(newMap, page)
        putNum(newMap, num)
        if (type == FROM_BUSINESS_FOLLOW) {
            putType(newMap, "2")
        }
        putSign(newMap)
        val meetingsAsync = BusinessApi.retrofitService.getMeetingsAsync(newMap)
        try {
            val meetingBean = meetingsAsync.await()
            getMeetings.getInfo(meetingBean)
        }catch (e:Exception) {
            Logger.e(e.toString())
            getMeetings.onError(e.toString())
        }
    }

    fun getMeetingDetail(id: String, getMeetingDetail: GetMeetingDetail) {
        val newMap = getNewMap()
        putId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.postMeetingDetail(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<MeetingDetailBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: MeetingDetailBean) {
                        getMeetingDetail.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    fun addOrModifyMeeting(id: String,
                           name: String,
                           province_id: String,
                           city_id: String,
                           area_id: String,
                           site: String,
                           start_time: String,
                           remark: String, getBase: GetBase) {
        val newMap = getNewMap()
        if (!id.isBlank()) {
            putId(newMap, id)
        }
        putUserId(newMap)
        putName(newMap,name)
        putProvinceId(newMap,province_id)
        putCityId(newMap,city_id)
        putAreaId(newMap,area_id)
        putSite(newMap,site)
        putStartTime(newMap,start_time)
        if (!remark.isBlank()) {
            putRemark(newMap,remark)
        }
        putSign(newMap)
        ApiService.getInstance().api.postInsertMeeting(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    fun meetingEnd(id:String, money:String, num: String, summary: String, getBase: GetBase) {
        val newMap = getNewMap()
        putId(newMap,id)
        if (money.isNotBlank()) {
            putMoney(newMap, money)
        }
        putNum(newMap,num)
        putSummary(newMap,summary)
        putSign(newMap)
        ApiService.getInstance().api.postMeetingOver(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    interface GetMeetings : BaseResponse {
        fun getInfo(meeting: MeetingBean)
    }

    interface GetMeetingDetail : BaseResponse {
        fun getInfo(meetingDetail: MeetingDetailBean)
    }
}